package cn.nuc.controller.system.back;

import cn.nuc.config.JWTUtils;
import cn.nuc.pojo.system.Manager;
import cn.nuc.pojo.entity.ResponseData;
import cn.nuc.pojo.system.User;
import cn.nuc.service.system.IManagerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/system/manager")
@Api(tags = "后台管理员")
public class ManagerController {
    @Autowired
    private IManagerService managerService;
    @PostMapping("/login")
    @ApiOperation(value = "后台登录" , notes = "使用用户名和密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名", name = "username", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "密码", name = "password", required = true, dataType = "String", paramType = "query")
    })
    public ResponseData<Manager> systemLogin(String username , String password){
        ResponseData responseData = null;
        List<Manager> managerList = managerService.list(new QueryWrapper<Manager>().eq("username", username));
        if(managerList != null){
            boolean checkpw = BCrypt.checkpw(password, managerList.get(0).getPassword());
            if(checkpw){
                responseData = ResponseData.success().putDataVule("admin" , managerList.get(0))
                                            .putDataVule("token" , JWTUtils.generToken(managerList.get(0).getId().toString() , "ncu@yuan" ,managerList.get(0).getUsername())) ;
            }else{
                responseData = ResponseData.cusstomerError("密码错误");
            }
        }else{
            responseData = ResponseData.cusstomerError("用户名不存在！");
        }
        return responseData;
    }
    @PutMapping("/saveOrUpdate")
    @ApiOperation(value = "更新管理员信息或添加新的管理员")
    @ApiImplicitParam(value = "要添加或者更新的管理员对象" , name = "manager" , required = true , dataType = "Manager" , paramType = "body")
    public ResponseData<System> addOrUpdate(@RequestBody Manager manager){
        if(manager.getId() == null){
            List<Manager> username = managerService.list(new QueryWrapper<Manager>().eq("username", manager.getUsername()));
            if(username.size() != 0){
                return ResponseData.cusstomerError("此用户名已存在");
            }
        }
        if(manager != null){
            String hashpw = BCrypt.hashpw(manager.getPassword(), BCrypt.gensalt());
            manager.setPassword(hashpw);
            boolean saveOrUpdate = managerService.saveOrUpdate(manager);
            if(saveOrUpdate){
                return ResponseData.success().putDataVule("saveOrUpdate" , saveOrUpdate);
            }
        }
        return ResponseData.serverInternalError();
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除管理员")
    @ApiImplicitParam(value = "删除的管理员ID"  ,name = "id" , required = true , dataType = "Integer" , paramType = "query")
    public ResponseData<Manager> deleteSystem(Integer id){
        boolean removeById = managerService.removeById(id);
        if(removeById){
            return ResponseData.success().putDataVule("deleteRes" , true);
        }
        return ResponseData.serverInternalError();
    }
    @GetMapping("/mangerList")
    @ApiOperation(value = "查询所有普通管理员")
    public ResponseData<Manager> getManagerList(String username){
        List<Manager> userList = null;
        if(username == null || StringUtils.isEmpty(username)){
            userList = managerService.list(new QueryWrapper<Manager>().eq("role" , "管理员"));
        }else{
            userList = managerService.list(new QueryWrapper<Manager>().like("username" , username).eq("role" , "管理员"));
        }
        if(userList != null){
            return ResponseData.success().putDataVule("managerList" , userList);
        }
        return ResponseData.serverInternalError();
    }
    @GetMapping("/getOne")
    public ResponseData<User> getOneById(Integer id){
        List<Manager> userList = managerService.list(new QueryWrapper<Manager>().eq("id", id));
        if(userList != null){
            return ResponseData.success().putDataVule("manager" , userList.get(0));
        }else{
            return  ResponseData.serverInternalError();
        }
    }
}
